﻿using MslDiary.ViewModel;
// 版权所有：2015  All Rights Reserved.
// 文 件 名：RichTextEditorViewModel.cs
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2015/1/18 13:22:51
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml;
using System.Xml.XPath;

namespace MslDiary.Control
{
    public class RichTextEditorViewModel : ViewModel.ViewModelBase
    {
        public RichTextEditorViewModel()
        {
            this.LoddingCommand = new CommandBase((Object parameter) => { Lodding(); });
        }

        #region 属性

        private static readonly string VisualFontFamiliesPath = @"/htmleditor/visualmode/fontfamilies/add/@value";
        private static readonly string SourceFontFamilyPath = @"/htmleditor/sourcemode/fontfamily/@value";
        private static readonly string SourceFontSizePath = @"/htmleditor/sourcemode/fontsize/@value";

        private List<string> _fontFamilyList;
        private List<FontSize> _fontSizeList;
        private int _fontSize = 12;
        private string _selectedFontFamily = "宋体";
        private string _selectedText;
        /// <summary>选中文本</summary>
        public string SelectedText
        {
            get { return _selectedText; }
            set
            {
                base.SetProperty<string>(ref this._selectedText, value);
            }
        }
        /// <summary>选中字体</summary>
        public string SelectedFontFamily
        {
            get { return _selectedFontFamily; }
            set
            {
                string txt = SelectedText;
                base.SetProperty<string>(ref this._selectedFontFamily, value);
            }
        }
        /// <summary>选中字体大小</summary>
        public int SelectFontSize
        {
            get { return _fontSize; }
            set
            {
                base.SetProperty<int>(ref this._fontSize, value);
            }
        }
        /// <summary>字体列表</summary>
        public List<string> FontFamilyList
        {
            get { return _fontFamilyList; }
            set
            {
                base.SetProperty<List<string>>(ref this._fontFamilyList, value);
            }
        }
        /// <summary>字体大小列表</summary>
        public List<FontSize> FontSizeList
        {
            get { return _fontSizeList; }
            set
            {
                base.SetProperty<List<FontSize>>(ref this._fontSizeList, value);
            }
        }
        #endregion

        #region 命令

        /// <summary>初始化</summary>
        public ICommand LoddingCommand { get; set; }
        #endregion

        #region 方法

        private void Lodding()
        {
            InitFontFamily();
            this.FontSizeList= new List<FontSize>()
            {
               FontSize.XXSmall,
               FontSize.XSmall,
               FontSize.Small,
               FontSize.Middle,
               FontSize.Large,
               FontSize.XLarge,
               FontSize.XXLarge
            };
        }

        private void InitFontFamily()
        {
            this.FontFamilyList = new List<string>();
            //FontFamily srcfamily = new FontFamily("Times New Roman");
            int srcsize = 10;

            try
            {
                using (XmlReader reader = XmlTextReader.Create(MslDiary.Framework.Gloable.StartPath + "\\res\\htmleditor.xml"))
                {
                    XPathDocument xmlDoc = new XPathDocument(reader);
                    XPathNavigator navDoc = xmlDoc.CreateNavigator();
                    XPathNodeIterator it;
                    it = navDoc.Select(VisualFontFamiliesPath);
                    while (it.MoveNext())
                    {
                        //FontFamily ff = new FontFamily(it.Current.Value);
                        this.FontFamilyList.Add(it.Current.Value);
                    }
                    it = navDoc.Select(SourceFontFamilyPath);
                    while (it.MoveNext())
                    {
                        //srcfamily = new FontFamily(it.Current.Value);
                        break;
                    }
                    it = navDoc.Select(SourceFontSizePath);
                    while (it.MoveNext())
                    {
                        srcsize = it.Current.ValueAsInt;
                        break;
                    }
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion
    }
}
